<?php


namespace app\admin\controller;

use app\common\controller\CommonController;
use app\common\model\UsersModel;
use app\admin\service\RbacService;
use app\common\service\Lang;
use app\common\validate\UsersValidate;
use app\common\exception\NotFoundException;
use app\common\exception\ParameterFailedException;
use app\common\service\Redis;
use Exception;
use think\Request;
use think\response\Json;

class AdminController extends CommonController
{
    /**
     * 用户登陆
     * @param  Request  $request
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws Exception
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/8/12
     * @Time: 14:54
     */
    public function login()
    {
        $params = self::getParam(['mobile_number', 'password']);
        (new UsersValidate())->goCheck('login', $params);

        // 验证账号密码
        $userModel = UsersModel::where('mobile_number', $params['mobile_number'])->field('id,password,salt')->find();
        if (!$userModel) {
            throw new ParameterFailedException(['msg' => '用户不存在']);
        }
        if (!decryptPassword($params['password'], $userModel->password, $userModel->salt)) {
            throw new ParameterFailedException(['msg' => '用户名与密码不匹配']);
        }

        $adminAccessToken = getToken();
        Redis::setAccessToken($adminAccessToken, $userModel->id,'admin');

        //返回token
        return retJson('登陆成功', ['admin_access_token' => $adminAccessToken]);
    }

    /**
     * @throws Exception
     */
    public function getAdminInfo()
    {
        $permissions = RbacService::checkPermissions('admin');
        $userModel   = UsersModel::where('id', $permissions['user_id'])->find();
        if (!$userModel) {
            throw new NotFoundException(['msg' => '该用户不存在']);
        }
        $res = $userModel->toArray();
        //foreach ($permissions['roles'] as $role) {
        //    $res['roles'][] = $role['']
        //}
        //;

        return retJson(Lang::GETDone, $res);
    }

    /**
     * 重置密码
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/7/7
     * @Time: 14:04
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function repwd()
    {
        //$params = self::getParam(['password', 'repassword', 'repwd_token', 'uid']);
        //(new UsersValidate())->goCheck(__FUNCTION__, $params);
        //
        //$uid   = (int)$params['uid'];
        //$token = Cache::get('user_credential_reset_pwd:'.$uid);
        //if (empty($uid) || empty($token) || $token !== $params['repwd_token']) {
        //    throw new ForbiddenException(['msg' => '您未验证']);
        //}
        //
        //$userModel = UsersModel::findOrEmpty($uid);
        //if ($userModel->isEmpty()) {
        //    throw new NotFoundException(['msg' => '用户不存在']);
        //}
        //$userModel->password = $usersService->set_password($params['password']);
        //if ($userModel->save()) {
        //    Cache::delete('user_credential_reset_pwd:'.$uid);
        //    return retJson('修改成功');
        //} else {
        //    return retJson('修改失败');
        //}
    }

}